cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _iptUserId:null,
        _txtInfo:null,
        _iptGemsNum:null,
        _targetData:null,
        
        _targetContent:null,
        _alertContent:null,
        _alert:null,
        
        _assignGems:0,
    },

    // use this for initialization
    onLoad: function () {
        this._iptUserId = cc.find("Canvas/page_wjck/ipt_uuid").getComponent(cc.EditBox);
        this._txtInfo = cc.find("Canvas/page_wjck/info").getComponent(cc.Label);
        this._ck = cc.find("Canvas/page_wjck/ck");
        this._ck.active = false;
        
        this._alert = cc.find("Canvas/page_wjck/alert");
        this._alert.active = false;
        
        this._iptGemsNum = cc.find("Canvas/page_wjck/ck/ipt_fk").getComponent(cc.EditBox);
        
        var btnSearch = cc.find("Canvas/page_wjck/btn_search");
        cc.addClickEvent(btnSearch,this.node,"PageWJCK","onBtnClicked");
        
        var btnApply = cc.find("Canvas/page_wjck/ck/btn_apply");
        cc.addClickEvent(btnApply,this.node,"PageWJCK","onBtnClicked");
        
        var btnOK = cc.find("Canvas/page_wjck/alert/btn_ok");
        cc.addClickEvent(btnOK,this.node,"PageWJCK","onBtnClicked");
        
        var btnCancel = cc.find("Canvas/page_wjck/alert/btn_cancel");
        cc.addClickEvent(btnCancel,this.node,"PageWJCK","onBtnClicked");
        
        this.node.on("switch_page",function(data){
           var pageName = data.detail;
           if(pageName == "wjck"){
               this._txtInfo.string = "充卡前请先查询玩家信息!";
               this._iptUserId.string = "";
               this._iptGemsNum.string = "";
               this._ck.active = false;
           }
        }.bind(this));
    },
    
    onBtnClicked:function(event){
        if(event.target.name == "btn_apply"){
            this.prepareAssign();
        }
        else if(event.target.name == "btn_search"){
            this.doSearch();
        }
        else if(event.target.name == "btn_ok"){
            this.doAssign(this._assignGems);
            this._assignGems = 0;
            this._alert.active = false;
        }
        else if(event.target.name == "btn_cancel"){
            this._assignGems = 0;
            this._alert.active = false;
        }
    },
    
    prepareAssign:function(){
        var assignGems = this._iptGemsNum.string;
        if(assignGems > cc.userdata.gems){
            cc.alert.show("提示","房卡不足!");
            return;
        }
        this._alert.active = true;
        var target = this._alert.getChildByName("target").getComponent(cc.Label);
        target.string = "为" + this._targetData.name + "(ID:"+ this._targetData.userid + ") 充值房卡";
        
        var content = this._alert.getChildByName("content").getComponent(cc.Label);
        content.string = "" + assignGems + "张";
        
        this._assignGems = assignGems;
    },
    
    doAssign:function(assignGems){
        var fn = function(ret){
            if(ret.errcode == 0){
                this.node.getComponent("UserInfo").setGems(ret.gems);
                this._targetData.gems = ret.targetgems;
                this._txtInfo.string = "ID:" + this._targetData.userid + "\n昵称:" + this._targetData.name + "\n当前房卡:" + this._targetData.gems;
                cc.alert.show("提示","充卡成功！");
            }
            else if(ret.errcode == 12580){
                cc.alert.show("提示","登录状态异常，需要重新登录!",function(){
                    this.node.emit("switch_page","login");    
                });
            }
            else{
                cc.alert.show("提示","充卡失败，请重试！");
            }
        };
        
        var data = {
            token:cc.userdata.token,
            userid:this._targetData.userid,
            num:assignGems,
        };
        cc.http.sendRequest("/assign_gems",data,fn.bind(this));
    },
    
    doSearch:function(){
        
        if(this._iptUserId.string == ""){
            cc.alert.show("提示","玩家ID不能为空!");
            return;
        }

        var fn = function(ret){
            console.log(ret);
            if(ret.errcode == 0){
                this._txtInfo.string = "ID:" + ret.userid + "\n昵称:" + ret.name + "\n当前房卡:" + ret.gems;
                this._ck.active = true;
                this._targetData = ret;
            }
            else if(ret.errcode == 12580){
                cc.alert.show("提示","登录状态异常，需要重新登录!",function(){
                    this.node.emit("switch_page","login");    
                });
            }
            else{
                this._txtInfo.string = "未获得相关信息";
            }
        };
        
        var data = {
            token:cc.userdata.token,
            userid:this._iptUserId.string,
        }
        this._txtInfo.string = "数据请求中...";
        this._ck.active = false;
        cc.http.sendRequest("/search_user",data,fn.bind(this));
    }
    

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
